var __JsonRequest = Class({
	Implements: Events,
	initialize: function() {
	},
	send : function(app, state, params) {
		var uri = 'index.php?query_id='+Xendri.nextQueryID()+'&query_type=json&application='+app+'&state='+state;
		var rq = new Request({url: uri, method: 'post', data: params});
		rq.addEvent('success', function(text, xml) {
			try {
				var res = JSON.decode(text);
			} catch (e) {
				Bro.log('Json error on state '+app+':'+state);
			}
			Bro.log('json: '+app+'_'+state);
			Xendri.JSON.fireEvent(app+'_'+state, res);
		});
		
		rq.send();
	},
	
	call:function(app,state, params, handler) {
		var uri = 'index.php?query_id='+Xendri.nextQueryID()+'&query_type=json&application='+app+'&state='+state;
		var rq = new Request({url: uri, method: 'post', data: params});
		rq.addEvent('success', function(text, xml) {
			try {
				var res = JSON.decode(text);
			} catch (e) {
				Bro.log('Json error on state '+app+':'+state);
			}
			Bro.log('json: '+app+'_'+state);
			if (typeof handler == 'function') handler(res);
		});
		
		rq.send();
	},
	
	onJsonFail: function(param) {
		alert('JsonFail');
	}
});

Xendri.JSON = new __JsonRequest(); 